package com.ttg.demo;

//~--- non-JDK imports --------------------------------------------------------

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ttg.utils.TLinx2Util;
import com.ttg.utils.TestParams;
import com.ttg.utils.sm.CryptoUtil;
import com.ttg.utils.sm2.SMUtil;

import java.util.*;

//~--- JDK imports ------------------------------------------------------------

//~--- classes ----------------------------------------------------------------

/**
 * 获取商户月平均交易 Merchant/month_avg
 */
public class MctMonthAvgDemo2 {


    private static final int LENGTH = 16;
    private static final String NUMBERS = "0123456789";

    public static String generateRandomNumber() {
        StringBuilder sb = new StringBuilder(LENGTH);
        Random random = new Random();
        for (int i = 0; i < LENGTH; i++) {
            int index = random.nextInt(NUMBERS.length());
            sb.append(NUMBERS.charAt(index));
        }
        return sb.toString();
    }


    public void getMctMonthAvg() {

        // 初始化参数
        String timestamp = new Date().getTime() / 1000 + "";    // 时间
        //随机生成对称加密SM4
        String secret =generateRandomNumber();
        System.out.println("secret====>"+secret);
        String appPriKey = TestParams.appPriKey.trim();
        String pubKey = TestParams.bankPubKey.trim();

        try {

            // 固定参数
            TreeMap<String, String> postmap = new TreeMap<String, String>();    // 请求参数的map

            postmap.put("channel_id", TestParams.CHANNEL_ID);
            postmap.put("timestamp", timestamp);

            TreeMap<String, Object> datamap = new TreeMap<String, Object>();



            /**
             * 对secret使用公钥进行SM2加密
             */
            byte[] resultData = SMUtil.sm2EncryptDataForPubKey(secret.getBytes("UTF-8"), pubKey, false);
            postmap.put("secret", CryptoUtil.bin2hex(resultData));


            TLinx2Util.handleEncrypt(datamap, postmap, secret);

            /**
             * 2 请求参数签名 按A~z排序，串联成字符串，先进行sha1加密(小写)，再进行md5加密(小写)，得到签名
             */
            TLinx2Util.handleSign(postmap,appPriKey);

            /**
             * 3 请求、响应
             */
            String rspStr = TLinx2Util.handlePost(postmap, TestParams.GET_FILE_LIST_URL);
            System.out.println("====post响应字符串= " + rspStr);

            /**
             * 4 验签  有data节点时才验签
             */
            JSONObject respObject = JSONObject.parseObject(rspStr);
            System.out.println("====响应错误码：" + respObject.get("errcode"));
            System.out.println("====响应错误提示：" + respObject.get("msg"));
            System.out.println("====响应data节点：" + respObject.get("data"));
            Object dataStr = respObject.get("data");

            Map<String,Object> respMap = TLinx2Util.handleDecrypt(dataStr.toString(), secret);
            System.out.println("====响应解密后data节点：" + JSONObject.toJSONString(respMap));
            for (Map.Entry<String, Object> entry : respMap.entrySet()) {

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Method main
     * Description 说明：
     *
     * @param args 说明：
     */
    public static void main(String[] args) {
        new MctMonthAvgDemo2().getMctMonthAvg();
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
